<template>
  <div class="login-wrapper">

    <input type="text" v-model="test">
    <div @click="senmessage()">asdasdasdasd</div>

  </div>
</template>

<script>
import storage from "./../utils/storage";
export default {
  name: "login",
  data() {
    return {
      test: "test",
      ws: null,
      user: {
        userName: "admin",
        userPwd: "123456"
      },
      rules: {
        userName: [
          {
            required: true,
            message: "请输入用户名",
            trigger: "blur"
          }
        ],
        userPwd: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur"
          }
        ]
      }
    };
  },
  mounted() {
    this.ws = new WebSocket("ws://localhost:9001");

    this.ws.onopen = () => {
      console.log("WebSocket onopen");
    };

    this.ws.onmessage = e => {
      console.log("========WebSocket onmessage start========");
      console.log("WebSocket message received：", e);
      console.log("WebSocket data received：", e.data);
      const reader = new FileReader();
      // Blob {size: 121, type: '',data:Blob }
      // 是否返回是二进制流 默认是Blob类型的
      const data = e.data;
      let result = null;
      if (data instanceof Blob) {
        reader.readAsText(data, "UTF-8");
        reader.onload = (e) => {
          // console.log(reader.result,typeof reader.result)
          // // if(typeof reader.result =='object'){
          // //   result=JSON.parse(reader.result);
          // // }
          result = (reader.result);
          console.log("WebSocket 接收到的数据是：", result);
          that.list.push(result);
        };
      }
      console.log("========WebSocket onmessage end=========");
    };

    this.ws.onclose = e => {
      console.log("WebSocket onclose");
    };
  },
  methods: {
    senmessage() {
      console.log("========WebSocket sendmessage start========");
      console.log("发送的内容是：1223123");
      console.log("========WebSocket sendmessage end========");

      console.log(this.test);
      this.ws.send(this.test);
    },
    login() {
      this.$refs.userForm.validate((valid) => {
        if (valid) {
          this.$api.login(this.user).then(async (res) => {
            this.$store.commit("saveUserInfo", res);
            await this.loadAsyncRoutes();
            this.$router.push("/welcome");
          });
        } else {
          return false;
        }
      });
    },
    async loadAsyncRoutes() {
      const userInfo = storage.getItem("userInfo") || {};
      if (userInfo.token) {
        try {
          const { menuList } = await this.$api.getPermissionList();
          const routes = utils.generateRoute(menuList);
          routes.map((route) => {
            const url = `./../views/${route.component}.vue`;
            route.component = () => import(url);
            this.router.addRoute("home", route);
          });
        } catch (error) { }
      }
    },
    goHome() {
      this.$router.push("/welcome");
    }
  }
};
</script>

<style lang="scss">
.login-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f9fcff;
  width: 100vw;
  height: 100vh;
  .modal {
    width: 500px;
    padding: 50px;
    background-color: #fff;
    border-radius: 4px;
    box-shadow: 0px 0px 10px 3px #c7c9cb4d;
    .title {
      font-size: 50px;
      line-height: 1.5;
      text-align: center;
      margin-bottom: 30px;
    }
    .btn-login {
      width: 100%;
    }
  }
}
</style>